package Server;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

/*
 * Author: Anthony Fleshner
 * 
 * Mouse listener to enable faster painting
 * 
 * When the mouse is pressed, the robot will draw at the mouse coordinates.
 * When the mouse is not pressed, the robot arm will move without painting.
 */

public class RobotMouseListener implements MouseMotionListener{
	
	int xPos;
	int yPos;
	
	ControlComponent control;
	RobotComponent robot;
	SocketServer server;
	
	RobotMouseListener(ControlComponent con){
		this.control = con;
		this.robot = con.getRobot();
		this.server = con.getRobot().getSocketServer();
		xPos = 0;
		yPos = 0;
	}

	@Override
	public void mouseDragged(MouseEvent arg0) {//Paints at the mouse coordinates when button is pressed
		
		if(robot.isMouseMove()){	
			xPos = arg0.getX();
			yPos = arg0.getY();
            
			robot.getLinkMatrices().inverse_kinematics(xPos-175, 500-yPos);
			robot.getNewLinks();
			//System.out.println("Mouse Coords Paint: " + xPos + " " + yPos);
			
			robot.setPaint(true);
			control.getPaintToggle().setSelected(true);
			
			try {
				server.serve("-1 11");
				if(xPos>0)
				server.serve(xPos + " " + yPos);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		robot.repaint();
	}

	@Override
	public void mouseMoved(MouseEvent arg0) {//Updates the mouse coordinates (moving the robot) without painting
		if(robot.isMouseMove()){	
			robot.setPaint(false);
			control.getPaintToggle().setSelected(false);
			
			xPos = arg0.getX();
			yPos = arg0.getY();
			robot.getLinkMatrices().inverse_kinematics(xPos-175, 500-yPos);
			robot.getNewLinks();
			//System.out.println("Mouse Coords Paint: " + xPos + " " + yPos);
			
			try {
				server.serve("-1 12");
				server.serve(xPos + " " + yPos);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		robot.repaint();
	}

	

}
